package com.mh.calculator;

import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private StartAppAd startAppAd = new StartAppAd(this);

	@Override
	public void onResume() {
		super.onResume();
		startAppAd.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		startAppAd.onPause();
	}

	@Override
	public void onBackPressed() {
		startAppAd.onBackPressed();
		super.onBackPressed();
	}

	public static Double number1;
	public static Double number2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		StartAppSDK.init(this, "108155264", "208572436", true);

		View someView = findViewById(R.layout.main);
		setContentView(R.layout.main);

		startAppAd.showAd(); // show the ad
		startAppAd.loadAd(); // load the next ad

		final Vibrator vibe = (Vibrator) this
				.getSystemService(Context.VIBRATOR_SERVICE);

		android.view.Display display = ((android.view.WindowManager) getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay();

		final EditText editText = (EditText) findViewById(R.id.editText1);
		final TextView textView = (TextView) findViewById(R.id.textView1);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width = dm.widthPixels;
		int height = dm.heightPixels;
		int dens = dm.densityDpi;
		double wi = (double) width / (double) dens;
		double hi = (double) height / (double) dens;
		double x = Math.pow(wi, 2);
		double y = Math.pow(hi, 2);
		double screenInches = Math.sqrt(x + y);
		//
		// textView.setText("width:" + width + " --- height:" + height
		// + " --- dens:" + dens + " --- wi:" + wi + " --- hi:" + hi
		// + " --- x" + x + " --- y:" + y + " --- screenInches:" +
		// screenInches);
		LayoutParams params = (LayoutParams) editText.getLayoutParams();
		params.height = (int) ((height / 100) * 25);
		editText.setLayoutParams(params);

		
		LinearLayout panel1=(LinearLayout) findViewById(R.id.panel1);
		LayoutParams params1 = (LayoutParams) panel1.getLayoutParams();
		params1.height = (int) ((height / 100) * 17);
		panel1.setLayoutParams(params1);
		
		LinearLayout panel2=(LinearLayout) findViewById(R.id.panel2);
		LayoutParams params2 = (LayoutParams) panel2.getLayoutParams();
		params2.height = (int) ((height / 100) * 17);
		panel2.setLayoutParams(params2);
		
		LinearLayout panel3=(LinearLayout) findViewById(R.id.panel3);
		LayoutParams params3 = (LayoutParams) panel3.getLayoutParams();
		params3.height = (int) ((height / 100) * 17);
		panel3.setLayoutParams(params3);
		
		LinearLayout panel4=(LinearLayout) findViewById(R.id.panel4);
		LayoutParams params4 = (LayoutParams) panel4.getLayoutParams();
		params4.height = (int) ((height / 100) * 17);
		panel4.setLayoutParams(params4);

		textView.setText(((height / 100) * 25) + " -- ");
		
		

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		final Animation animAlpha = AnimationUtils.loadAnimation(this,
				R.anim.anim_alpha);

		Button btnplus = (Button) findViewById(R.id.button102);
		btnplus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				number1 = Double.parseDouble(editText.getText().toString());
				textView.setText(editText.getText() + " + ");

				editText.setText("");

			}
		});

		Button btnmini = (Button) findViewById(R.id.Button01);
		btnmini.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				number1 = Double.parseDouble(editText.getText().toString());
				textView.setText(editText.getText() + " - ");

				editText.setText("");

			}
		});

		Button btndivi = (Button) findViewById(R.id.Button011);
		btndivi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				number1 = Double.parseDouble(editText.getText().toString());
				textView.setText(editText.getText() + " % ");

				editText.setText("");

			}
		});

		Button btnmulti = (Button) findViewById(R.id.button101);
		btnmulti.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				number1 = Double.parseDouble(editText.getText().toString());
				textView.setText(editText.getText() + " X ");

				editText.setText("");

			}
		});

		Button btnequal = (Button) findViewById(R.id.button10);
		btnequal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				number2 = Double.parseDouble(editText.getText().toString());

				if (textView.getText().toString().contains("+")) {
					Double result = number1 + number2;

					textView.setText(textView.getText() + ""
							+ editText.getText() + " = " + result);

					editText.setText(result.toString());

				}

				if (textView.getText().toString().contains("X")) {
					Double result = number1 * number2;

					textView.setText(textView.getText() + ""
							+ editText.getText() + " = " + result);

					editText.setText(result.toString());

				}

				if (textView.getText().toString().contains("%")) {
					Double result = number1 % number2;

					textView.setText(textView.getText() + ""
							+ editText.getText() + " = " + result);

					editText.setText(result.toString());

				}

				if (textView.getText().toString().contains("-")) {
					Double result = number1 - number2;

					textView.setText(textView.getText() + ""
							+ editText.getText() + " = " + result);

					editText.setText(result.toString());

				}

			}
		});

		Button btnc2 = (Button) findViewById(R.id.button11);
		btnc2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();

				textView.setText(editText.getSelectionStart() + "");

			}
		});

		Button btnc = (Button) findViewById(R.id.button6);
		btnc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText("");
				textView.setText("");
				number1 = 0.0;
				number2 = 0.0;

			}
		});

		Button btn7 = (Button) findViewById(R.id.button3);
		btn7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "7");

			}
		});

		Button btn8 = (Button) findViewById(R.id.button4);
		btn8.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "8");

			}
		});

		Button btn9 = (Button) findViewById(R.id.button5);
		btn9.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "9");

			}
		});

		Button btn4 = (Button) findViewById(R.id.button202);
		btn4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "4");

			}
		});

		Button btn5 = (Button) findViewById(R.id.button222);
		btn5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "5");

			}
		});

		Button btn6 = (Button) findViewById(R.id.button91);
		btn6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "6");

			}
		});

		Button btn1 = (Button) findViewById(R.id.button21);
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "1");

			}
		});

		Button btn2 = (Button) findViewById(R.id.button223);
		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "2");

			}
		});

		Button btn3 = (Button) findViewById(R.id.button9);
		btn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "3");

			}
		});

		Button btn0 = (Button) findViewById(R.id.button20);
		btn0.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + "0");

			}
		});

		Button btncol = (Button) findViewById(R.id.button22);
		btncol.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				vibe.vibrate(100);
				view.startAnimation(animAlpha);
				MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.click);
				mediaPlayer.start();
				editText.setText(editText.getText() + ".");

			}
		});

	}
}
